//
//  ChannelContentView.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/10.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "ChannelContentView.h"
#import "ChannelCellFrameViewModel.h"
#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "ChannelContent.h"
#import "ChannelImg.h"
#import "ChannelTags.h"
#import "ChannelVoice.h"
#import "NSString+Time.h"

#import "TAMacro.h"

@interface ChannelContentView ()

// 头像
@property (nonatomic, weak) UIButton *headerImageButton;
// vip图片
@property (nonatomic, weak) UIImageView *vipImageView;
// 用户昵称
@property (nonatomic, weak) UILabel *nameLabel;
// 主要图片
@property (nonatomic, weak) UIImageView *contentImageView;
// 文本
@property (nonatomic, weak) UILabel *descLabel;
// 标签
@property (nonatomic, weak) UILabel *tagsLabel;
// 图片个数
@property (nonatomic, weak) UILabel *imgNumLabel;
// 时间
@property (nonatomic, weak) UILabel *totalTimeLabel;
// 播放按钮
@property (nonatomic, weak) UIButton *playButton;


@end


@implementation ChannelContentView


- (instancetype) initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // 添加所有子控件的方法
        [self setUpAllChildViews];
        
    }
    return self;
}


// 添加所有子控件的方法
- (void)setUpAllChildViews {
    
    // 头像
    UIButton *headerImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:headerImageButton];
    [headerImageButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    _headerImageButton = headerImageButton;
    
    // vip图片
    UIImageView *vipImageView = [[UIImageView alloc] init];
    [self addSubview:vipImageView];
    _vipImageView = vipImageView;
    
    // 用户昵称
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.font = NameFont;
    [self addSubview:nameLabel];
    _nameLabel = nameLabel;
    
    // 主要图片
    UIImageView *contentImageView = [[UIImageView alloc] init];
    [self addSubview:contentImageView];
    _contentImageView = contentImageView;
    
    // 文本
    UILabel *descLabel = [[UILabel alloc] init];
//    descLabel.backgroundColor = [UIColor cyanColor];
    descLabel.numberOfLines = 0;
    descLabel.font = TextFont;
    descLabel.textColor = [UIColor grayColor];
    [self addSubview:descLabel];
    _descLabel = descLabel;
    
    // 标签
    UILabel *tagsLabel = [[UILabel alloc] init];
//    tagsLabel.backgroundColor = [UIColor greenColor];
    tagsLabel.font = TagsFont;
    tagsLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:tagsLabel];
    _tagsLabel = tagsLabel;
    
    // 图片个数
    UILabel *imgNumLabel = [[UILabel alloc] init];
    imgNumLabel.numberOfLines = 1;
    imgNumLabel.textAlignment = 1;
    imgNumLabel.font = TextFont;
    imgNumLabel.textColor = [UIColor whiteColor];
    imgNumLabel.layer.borderWidth = 1;
    imgNumLabel.layer.borderColor = [UIColor whiteColor].CGColor;
    imgNumLabel.backgroundColor = [UIColor colorWithRed:40 / 255.0 green:43 / 255.0 blue:54 / 255.0 alpha:1];
    [self addSubview:imgNumLabel];
    _imgNumLabel = imgNumLabel;
    
    
    // 总时长
    UILabel *totalTimeLabel = [[UILabel alloc] init];
//    totalTimeLabel.backgroundColor = [UIColor blackColor];
    totalTimeLabel.font = [UIFont systemFontOfSize:30 weight:5];
    totalTimeLabel.textColor = [UIColor whiteColor];
    [self addSubview:totalTimeLabel];
    _totalTimeLabel = totalTimeLabel;
    
    
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    playButton.backgroundColor = [UIColor blackColor];
    [playButton setImage:[UIImage imageNamed:@"MusicPlayer_暂停"] forState:UIControlStateNormal];
    [self addSubview:playButton];
    _playButton = playButton;
    
    
    
}
// 头像的点击方法, 用block传到外面进行页面跳转
- (void)clickAction:(UIButton *)button {
    self.clickAction();
}
- (void)setChannelCellFrame:(ChannelCellFrameViewModel *)channelCellFrame {
    _channelCellFrame = channelCellFrame;
    
    [self setUpData];
    [self setUpFrame];
    
}

// 赋frame
- (void)setUpFrame {
    
    _headerImageButton.frame = _channelCellFrame.headerImageViewFrame;
    _nameLabel.frame = _channelCellFrame.nameLabelFrame;
    _vipImageView .frame = _channelCellFrame.vipImageViewFrame;
    _contentImageView.frame = _channelCellFrame.contentImageViewFrame;
    _descLabel.frame = _channelCellFrame.descLabelFrame;
    _tagsLabel.frame = _channelCellFrame.tagsLabelFrame;
    _imgNumLabel.frame = _channelCellFrame.imgNumLabelFrame;
    if ([_channelCellFrame.channelContent.postType isEqualToString:@"Voice"]) {
        
        _totalTimeLabel.frame = CGRectMake(30, CGRectGetMaxY(_contentImageView.frame) - 75, 150, 50);
        _playButton.frame = CGRectMake(SCREEN_WIDTH - 80, CGRectGetMaxY(_contentImageView.frame) - 75, 40, 40);
        
    }
    
}

// 传值
- (void)setUpData {
    
    [_headerImageButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _channelCellFrame.channelContent.avatar]] forState:UIControlStateNormal];
    
    _nameLabel.text = _channelCellFrame.channelContent.uname;
    _descLabel.text = _channelCellFrame.channelContent.desc;
    
    if (_channelCellFrame.channelContent.tag.count != 0) {
        NSMutableArray *tagNameArray = [NSMutableArray array];
        for (NSInteger i = 0; i < _channelCellFrame.channelContent.tag.count; i++) {
            [tagNameArray addObject:_channelCellFrame.channelContent.tag[i].content];
        }
        [tagNameArray insertObject:@" #" atIndex:0];
        NSString *tagString = [tagNameArray componentsJoinedByString:@" "];
        _tagsLabel.text = tagString;
    }

    _vipImageView.image = [UIImage imageNamed:@"vipAvatar"];
    if (_channelCellFrame.channelContent.isTalent == 0) {
        _vipImageView.hidden = YES;
    } else {
        _vipImageView.hidden = NO;
    }
    
    
    
    [_contentImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _channelCellFrame.channelContent.img[0].f]]];
    _contentImageView.contentMode = UIViewContentModeScaleAspectFill;
    _contentImageView.layer.masksToBounds = YES;
    //    NSLog(@"%ld", _actionImgArray.count);
    if (_channelCellFrame.channelContent.img.count <= 1) {
        _imgNumLabel.hidden = YES;
    } else {
        _imgNumLabel.hidden = NO;
        _imgNumLabel.text = [NSString stringWithFormat:@"%ld 图",  _channelCellFrame.channelContent.img.count];
    }
    
    
    if ([_channelCellFrame.channelContent.postType isEqualToString:@"Voice"]) {
        _totalTimeLabel.text = [NSString convertTime:_channelCellFrame.channelContent.voice.length];
    }

    
}






@end
